<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<script>
    // 创建空数组 三名玩家和牌库
    let poker = new Array();
    let player1 = new Array();
    let player2 = new Array();
    let player3 = new Array();
    //构造函数 用于创建54张牌的对象
    function Poker(type, num) {
        this.type = type;
        this.num = num;
    }
    // 分别将前54张牌对象存入数组
    for (let i = 0; i < 13; i++) {
        for (let j = 0; j < 4; j++) {
            if (j == 0) {
                poker[poker.length] = new Poker('红桃', i + 1);
            } else if (j == 1) {
                poker[poker.length] = new Poker('黑桃', i + 1);
            } else if (j == 2) {
                poker[poker.length] = new Poker('方块', i + 1);
            } else if (j == 3) {
                poker[poker.length] = new Poker('梅花', i + 1);
            }
        }
    }
    poker[poker.length] = new Poker('小王', 0);
    poker[poker.length] = new Poker('大王', 0);
    // 分别揭牌 共51（54-3）次，留三张底牌
    for (let i = poker.length - 1; i >= 3; i--) {
        // 0-54的一个随机数
        if (i % 3 == 0) {
            random = Math.floor(Math.random() * (i + 1));
            player1[player1.length] = poker[random];
            poker.splice(random, 1);
        } else if (i % 3 == 1) {
            random = Math.floor(Math.random() * (i + 1));
            player2[player2.length] = poker[random];
            poker.splice(random, 1);
        } else if (i % 3 == 2) {
            random = Math.floor(Math.random() * (i + 1));
            player3[player3.length] = poker[random];
            poker.splice(random, 1);
        }
    }
    // 整理各个玩家的手牌
    player1.sort((a, b) => {
        return a.num - b.num;
    })
    player2.sort((a, b) => {
        return a.num - b.num;
    })
    player3.sort((a, b) => {
        return a.num - b.num;
    })
    // 如果牌的数字为1，11，12，13 则将其数字换成A,J,Q,K 大小王不输出数字
    function changeNum(arr) {
        for (let i = 0; i < arr.length; i++) {
            let num;
            if (arr[i].num == 1) {
                num = 'A';
            } else if (arr[i].num == 11) {
                num = 'J';
            } else if (arr[i].num == 12) {
                num = 'Q';
            } else if (arr[i].num == 13) {
                num = 'K';
            } else if (arr[i].num == 0) {
                num = '';
            } else {
                num = arr[i].num;
            }
            console.log(arr[i].type + num);
        }
    }
    // 打印出三玩家的牌
    console.log('底牌:');
    changeNum(poker);

    console.log('玩家一:');
    changeNum(player1);

    console.log('玩家二:');
    changeNum(player2);

    console.log('玩家三:');
    changeNum(player3);
</script>

  
    




<body> 

</body>

</html>